package com.sbr.esbmonitor.activemq.repository.jmx;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.esbmonitor.servicemix.Server;

public class JMXManager {

	private static final Logger LOG = LoggerFactory.getLogger(JMXManager.class);

	public MBeanServerConnection connect(Server server) {
		JMXServiceURL url = null;
		try {
			url = new JMXServiceURL(server.getJmxUrl());
		} catch (MalformedURLException e) {
			LOG.error("Erreur dans le format de l'URL",e);
		}
		JMXConnector jmxc = null;
		HashMap<String,Object> env = new HashMap<String,Object>();
		env.put(JMXConnector.CREDENTIALS,new String[] {server.getUserName(), server.getPassword()});
		MBeanServerConnection mbsc = null;
		try {
			jmxc = JMXConnectorFactory.connect(url, env);
			mbsc = jmxc.getMBeanServerConnection();
		} catch (IOException e) {
			LOG.error("Erreur à la connexion JMX",e);
		}
		
		return mbsc;
	}

}
